package repository;

import java.util.List;




import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;

import model.ContatoBeijamim;
import utility.DAO;
import utility.HibernateUtil;

public class ContatoRepository implements DAO<ContatoBeijamim>{

	private Session session;
	
	@Override
	public void cadastrar(ContatoBeijamim objeto) {

		session = HibernateUtil.getSessionFactory().getCurrentSession(); 
		session.beginTransaction(); 
		session.save(objeto); 
		session.getTransaction().commit();
		
	}

	@Override
	public List<ContatoBeijamim> listar() {
		
		session = HibernateUtil.getSessionFactory().openSession(); 
		session.beginTransaction(); 
		List l1 = session.createQuery("from tb_contatos order by nome").list(); 
		session.getTransaction().commit(); 
		
		return l1;
	}

	@Override
	public void alterar(ContatoBeijamim objeto) {
		
		session = HibernateUtil.getSessionFactory().getCurrentSession(); 
		session.beginTransaction(); 
		session.update(objeto); 
		session.getTransaction().commit();
		
	}

	@Override
	public void remover(ContatoBeijamim objeto) {

		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction(); 
		session.delete(objeto); 
		session.getTransaction().commit();

	}

	@Override
	public ContatoBeijamim buscar(Long id) {

		session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction(); 
		ContatoBeijamim contato = (ContatoBeijamim)session.get(ContatoBeijamim.class, id); //
		session.getTransaction().commit(); 
		
		return contato;
	}

}
